function ProjectOffline() {
    var _this = this;
    var page;
    _this.page_size = 10;

    // init
    _this.init = function(_page) {
        page = _page;
        _this.initTable();
        _this.initEvent();
    }

    _this.initTable = function() {
        var list_url = appPath + "/admin/projectOffline/projectList";
        page.init("projectOfflineForm", list_url, "pro_tbody", "pro_page", 1, _this.page_size);
        page.goPage(1);
        page.list = function(dataList){
            _this.obj = dataList;
            if(dataList == null){
                return;
            }
            var len = dataList.length;
            var inner = "", item;
            // 组装数据
            for(var i=0; i< len; i++) {
                item = dataList[i];
                // 组装数据
                inner += "<tr>";
                inner += '<td width="50"><input type="checkbox" name="proBox" value="' + item.id +'" template="';
                if(item.hasOwnProperty('estimateReport')){
                    inner += "template";
                }
                inner += '"></td>';
                inner += "<td>"+(parseInt(i)+1)+"</td>";
                inner += "<td><span class=\"text-orange tooltip\" data-length='30'>"+item.projectName+"</span></td>";
                inner += "<td>"+(TimeUtil.longMsTimeConvertToDateTime(item['projectStartTime'])+"<br>"+TimeUtil.longMsTimeConvertToDateTime(item['projectEndTime']))+"</td>";
                inner += "<td>"+item.personCount+"</td>";
                inner += "<td>"+Enum.projectOfflineType(item.projectType)+"</td>";
                inner += "<td><span class=\"text-blue\">" +Enum.projectStatus(item.projectStatus)+ "</span></td>";
                inner += "<td>"+item.createUser+"</td>";
                inner += "<td>"+TimeUtil.longMsTimeConvertToDateTime(item.createTime)+"</td>";
                inner += '<td><a href="javascript:projectOffline.projectInfo(\''+item.id+'\');" class="a a-view">详情</a> ';
                if(2 == item.projectStatus && item.permission ){
                    inner += '<a href="javascript:projectOffline.projectInfoModify(\''+item.id+'\');" class="a a-info">修改</a> ';
                    inner += '<a href="javascript:projectOffline.del(\''+item.id+'\');" class="a a-view">删除</a> ';
                }else {
                    inner += '<a href="javascript:" class="a">修改</a> ';
                    inner += '<a href="javascript:" class="a">删除</a> ';
                }
                if((4 == item.projectStatus || 3 == item.projectStatus) && item.permission ){
                    inner += '<a href="javascript:projectOffline.datumManage(\''+item.id+'\');" class="a a-info">资料管理</a>';
                    inner += '<a href="javascript:projectOffline.processManage(\''+item.id+'\');" class="a a-view">过程管理</a> ';
                    inner += '<a href="javascript:projectOffline.exportData(\''+item.id+'\',\''+ item.projectName +'\',\''+item.personCount+'\');" class="a a-info">导出</a> ';
                }else{
                    // inner += '<a href="javascript:" class="a">过程管理</a> ';
                    // inner += '<a href="javascript:" class="a">导出</a> ';
                }
                // 评估总结
                if(item.hasOwnProperty('estimateReport')){
                    inner +=  '<a href="javascript:projectOffline.estimateReport(\''+item.id+'\',\''+ item.projectStatus+'\');"  class="a a-info">评估总结</a>';
                }
                inner += "</td>";
                inner += "</tr>";
            }
            return inner;
        }

    };

    _this.initEvent = function () {
        $('#click_project_status').on("click", "span", function(){
            $('#click_project_status').find("span").removeClass("active");
            $(this).addClass("active");
            bind_click(this);
        });
        //搜索
        $("#project_search").click(function () {
            _this.initTable();
        });

        //全部
        $("#project_all").click(function () {
            $('#click_project_status').find("span").removeClass("active");
            $('#click_project_status span:eq(0)').addClass("active");
            $("#projectStatus").val("");
            $("#projectStartTime").val("");
            $("#projectEndTime").val("");
            $("#projectName").val("");
            _this.initTable();
        });
    };

    bind_click = function(t){
        var value = $(t).attr("data-value");
        $("#projectStatus").val(value);
        _this.initTable(page);
    };


    // 项目详情
    _this.projectInfo = function (projectId) {
        var url = appPath + "/admin/projectOffline/projectInfo?projectId="+projectId;
        window.open(url,"_blank");
    }
    // 项目详情修改
    _this.projectInfoModify = function (projectId) {
        var url = appPath + "/admin/projectOffline/projectInfoModify?projectId="+projectId;
        window.open(url,"_self");
    }

    //删除项目
    _this.del = function (projectId) {
        layer.confirm('确定要删除选择的项目吗？', {btn: ['是', '否']}, function (index) {
            layer.close(index);
            $.ajax({
                url: appPath + "/admin/projectOffline/delete",
                data: {
                    "projectIds": projectId
                },
                dataType: "json",
                async: true,
                type: "POST",
                success: function (data) {
                    var result = data.code;
                    if (10000 == result) {
                        layer.alert(data.message , {icon : 1, closeBtn:0},function (index) {
                            layer.close(index);
                            //刷新课程列表
                            _this.initTable();
                        });
                    } else {
                        layer.alert(data.message, {icon : 2});
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.msg(textStatus, {icon: 2});
                }
            });
        }, function () {

        });
    }

    // 过程管理
    _this.processManage = function (projectId) {
        var url = appPath + "/admin/projectOffline/processManage?projectId="+projectId;
        window.open(url,"_blank");
    }

    // 资料管理
    _this.datumManage = function (projectId) {
        window.location.href = appPath + "/admin/projectOffline/datumManage?projectId="+projectId;
    }

    // 导出学员一览表
    _this.exportData = function (projectId, projectName, personCount) {
        if(0 == personCount){
            layer.alert("该项目下暂无学员",{icon:7});
        }else{
            var date = new Date();
            var currentDate =date .getFullYear()+"-"+date.getMonth()+"-"+date.getDay();
            // 文件名
            var fileName = projectName+ currentDate + ".xlsx";
            window.location.href = appPath + '/admin/projectOffline/exportProjectInfo?projectId='+ projectId+"&fileName=" + fileName + "&projectName=" + projectName;
        }
    }

    // 创建项目
    _this.createOfflineProject = function () {
        window.location.href = appPath + "/admin/projectOffline/createOfflineProject";
    }

    // 批量删除项目
    _this.deleteProject = function () {
        var projectIds=[]
        $('#pro_tbody input:checked').each(function(i,e){
            projectIds.push($(this).val());
        });
        if(projectIds.length==0){
            layer.msg("未选择项目");return;
        }
        //校验所选项目是否只包含未开始项目
        $.ajax({
            url: appPath + "/admin/projectOffline/ifOnlyNotBegin",
            data: {
                "projectIds": projectIds.join(",")
            },
            dataType: "json",
            async: true,
            type: "POST",
            success: function (data) {
                var result = data.code;
                if (10000 == result) {
                    //删除
                    _this.del(projectIds.join(","));
                } else {
                    layer.alert(data.message, {icon : 2});
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.msg(textStatus, {icon: 2});
            }
        });
    }

    // 导入项目
    _this.importProject = function () {
        layer.open({
            type: 2,
            title: '导入excel',
            shadeClose: false,				//true点击遮罩层关闭
            shade: 0.3,					//遮罩层背景
            maxmin: true, 					// 开启最大化最小化按钮
            area: ['1000px', '80%'],		//弹出层大小
            scrollbar: false,				//false隐藏滑动块
            content: [appPath + '/admin/projectOffline/to_import?tempType', 'yes'],
            cancel: function () {
                //刷新数据
                _this.initTable();
            }
        })
    }

    //弹窗-选择模板
    _this.chooseTemplate = function() {
        var projectIds=[]
        $('#pro_tbody input:checked').each(function(i,e){
            projectIds.push($(this).val());
        });
        if(projectIds.length==0 || projectIds.length > 1){
            layer.msg("请选择一个项目");return;
        }
        //先判断是否可选择模板
        $.ajax({
            url: appPath + '/project/estimateQuestion/changeTemplate',
            async: false,
            type: 'post',
            data: {'projectId': projectIds[0]},
            success: function (data) {
                if (data.code == 10000) {
                    layer.open({
                        type: 2,
                        title: '效果评估模板选择',
                        area: ['70%', '80%'],		//弹出层大小
                        scrollbar: false,				//false隐藏滑动块
                        content: [appPath + '/admin/estimate/choose?projectId=' + projectIds[0] + '&projectType=offline', 'yes']
                    });
                }else{
                    layer.msg(data.message);
                }
            }
        });
    };

    // 导出评估结果
    _this.exportReport = function() {
        var projectIds=[]
        $('#pro_tbody input:checked').each(function(i,e){
            projectIds.push($(this).val());
        });
        if(projectIds.length==0 || projectIds.length > 1){
            layer.msg("请选择一个项目");return;
        }
        var obj = $('#pro_tbody input:checked');
        var template = obj.attr("template");
        // 已选择模板， 导出评估结果
        if('' != template){
            window.location.href = appPath + '/admin/projectEstimate/exportReport?projectId=' + projectIds[0]+'&projectType=offline';
        }else{
            layer.msg("当前项目未设置评估模板");
        }
    }

    _this.estimateReport = function(projectId, projectStatus) {
        if('3' == projectStatus || '4' == projectStatus){
            var url = appPath + "/admin/projectEstimate/report?projectId="+projectId+'&projectType=offline';
            window.open(url,"_blank");
        }else{
            layer.msg('项目尚未开始，暂无评估结果');
        }

    }
}

var projectOffline = new ProjectOffline();